package debtbundler.presentation.preferences;


import static org.junit.Assert.*;

import java.io.File;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UserPreferencesTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		debtbundler.logging.ConfigurationReader.setDefaultConf();
	}

	@Before
	public void setUp() throws Exception {
		Preferences prefs = UserPreferences.getUserPreferences();
		prefs.clear();
	}

	@After
	public void tearDown() throws Exception {
		Preferences prefs = UserPreferences.getUserPreferences();
		prefs.clear();
	}
	
	/**
	 * Tests getting and setting preferences when the current 
	 * user initially has no recent files at all.
	 * <p>
	 * Note that running this will clear the current OS user's 
	 * own preferences. I don't know how to change the current user
	 * runtime. System.setProperty("user.name", "test_user") does
	 * not do it.
	 * 
	 * 
	 * @throws BackingStoreException 
	 */
	@Test
	public void testUserPreferences() throws BackingStoreException {
		assertEquals("Test user initially has no recent files", 0, UserPreferences.getRecentFiles().length);
		File file1 = new File("C:\test\test1.txt");
		UserPreferences.setMostRecentFile(file1);
		assertEquals("User has 1 recent file", 1, UserPreferences.getRecentFiles().length);
		
		UserPreferences.setMostRecentFile(file1);
		assertEquals("User still has 1 recent file when same file is set as most recent", 1, UserPreferences.getRecentFiles().length);
		
		File file2 = new File("C:\test\test2.txt");
		UserPreferences.setMostRecentFile(file2);
		assertEquals("User has 2 recent files", 2, UserPreferences.getRecentFiles().length);
		
		File file3 = new File("C:\test\test3.txt");
		UserPreferences.setMostRecentFile(file3);
		assertEquals("User has 3 recent files", 3, UserPreferences.getRecentFiles().length);
		
		File file4 = new File("C:\test\test4.txt");
		UserPreferences.setMostRecentFile(file4);
		assertEquals("User has at most 3 recent files", 3, UserPreferences.getRecentFiles().length);
		
		String[] recentFiles = UserPreferences.getRecentFiles();
		assertEquals("Correct order of recent files", file4.getAbsolutePath(), recentFiles[0]);
		assertEquals("Correct order of recent files", file3.getAbsolutePath(), recentFiles[1]);
		assertEquals("Correct order of recent files", file2.getAbsolutePath(), recentFiles[2]);
	}
	

}
